import QtQuick 1.1
import com.nokia.meego 1.0

Dialog {
    id: itemDetailsDialog

    property Style platformStyle: QueryDialogStyle {}

    property string itemName
    property string message
    property string unit

    property string acceptButtonText
    property string rejectButtonText

    property double quantity

    signal accepted(string quantity, string unit)

    content:
        Column{

        anchors.horizontalCenter: parent.horizontalCenter
        width: parent.width
        spacing: itemDetailsDialog.platformStyle.buttonsColumnSpacing

        Row{
            width: parent.width
            height: titleText.height

            Text{
                id: titleText

                text: itemName
                width: parent.width

                font.family: itemDetailsDialog.platformStyle.titleFontFamily
                font.pixelSize: itemDetailsDialog.platformStyle.titleFontPixelSize
                font.bold:  itemDetailsDialog.platformStyle.titleFontBold
                font.capitalization: itemDetailsDialog.platformStyle.titleFontCapitalization
                color: itemDetailsDialog.platformStyle.titleTextColor

                horizontalAlignment: Text.AlignHCenter
                verticalAlignment:   Text.AlignVCenter
            }
        }

        Row{
            Text{
                text: message

                font.family: itemDetailsDialog.platformStyle.messageFontFamily
                font.pixelSize: itemDetailsDialog.platformStyle.messageFontPixelSize
                color: itemDetailsDialog.platformStyle.messageTextColor

                verticalAlignment: Text.AlignVCenter
            }
        }

        Row{
            id: row
            width: parent.width

            spacing: 10

            QuantityPicker{
                id: quantityPicker

                width: parent.width

                minValue: 1
                divider: unit=="kom"?1:10
                value: unit=="kom"?quantity:quantity*quantityPicker.divider
                maxValue: unit=="kom"?100:1000
            }
        }

        ButtonRow {
            id: unitButtons
            //anchors.top: row.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            Repeater {
                id:rep
                model: modelUnits

                Button{
                    text: (name=="kom")?qsTr("kom"):name //tu tek prevodimo mjerne jedinice
                    checked: name==unit
                    onClicked:{
                        var oldUnit=unit
                        var oldDivider=quantityPicker.divider
                        var value=quantityPicker.value
                        unit=name

                        if (unit=="kom"&&oldUnit!="kom"){
                            quantityPicker.value=Math.round(value/oldDivider)
                        }
                        else if (oldUnit=="kom"){
                            quantityPicker.value=Math.round(value*quantityPicker.divider)
                        }
                    }
                    platformStyle: ButtonStyle {inverted: true}
                }
            }
        }

    }

    buttons:
        //The following code is taken from QueryDialog.qml

        Item {
        id: buttonColFiller
        width: parent.width
        height: childrenRect.height

        anchors.top: parent.top

        Item {id: dummy; anchors.fill:  parent}

        Column {
            id: buttonCol
            anchors.top: parent.top
            anchors.topMargin: itemDetailsDialog.platformStyle.buttonTopMargin
            spacing: itemDetailsDialog.platformStyle.buttonsColumnSpacing

            height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                    + (rejectButton.text == "" ? 0 : rejectButton.height)
                    + anchors.buttonTopMargin  + spacing

            anchors.horizontalCenter: parent.horizontalCenter

            Button {
                id: acceptButton
                text: acceptButtonText
                onClicked: {
                    accepted(quantityPicker.realValue, unit)
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
            Button {
                id: rejectButton
                text: rejectButtonText
                onClicked: {
                    itemDetailsDialog.destroy()
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
        }
    }
}
